package com.py.credit.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class LonAndLatUtil {
	
	public static String UrlConn(String url,List<NameValuePair> list){
		//创建httpclient对象
    	
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //代理对象
        //创建请求方法的实例， 并指定请求url
        HttpPost httppost=new HttpPost(url);
        /* 
         * 添加请求头信息
         */
        // 浏览器表示
        httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        // 传输的类型
        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        
        //使用配置
        /*List<NameValuePair> list = new ArrayList<NameValuePair>();  
        list.add(new BasicNameValuePair("ds", "{\"keyWord\":\"延庆区北京市延庆区延庆镇莲花池村前街50夕阳红养老院\"}"));  
        HttpEntity param = new UrlEncodedFormEntity(list, "utf-8");  */
        //httppost.setEntity(getEntity(list));
        CloseableHttpResponse response=null;
        HttpEntity entity=null;
        String content=null;
        try{
        	response=httpClient.execute(httppost);
        	entity=response.getEntity();
        	content=EntityUtils.toString(entity);
        }catch(Exception e){
        	
        }finally{
        	try {
				httpClient.close();
			} catch (IOException e) {
				return null;
			}
        }
        return content;
	}

	public static HttpEntity getEntity(List<NameValuePair> list){
        HttpEntity param;
		try {
			param = new UrlEncodedFormEntity(list, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}  
        return param;
	}
	
	public static String getConn(String url) {
		CloseableHttpClient client = HttpClients.createDefault();//获取http客户端
		HttpGet httpGet = new HttpGet(url);
		httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
		httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
		try {
		CloseableHttpResponse response = client.execute(httpGet);
		HttpEntity entity = response.getEntity();
		String info = EntityUtils.toString(entity, "UTF-8");
		return info;
	 }catch(Exception e){
     	
     }finally{
     	try {
     		client.close();
			} catch (IOException e) {
				return null;
			}
     }
		return null;
	}
//	public static void main(String[] args) throws ClientProtocolException, IOException {
//		getConn("北京市顺义区仁和地区河南村村委会南1500米","0bcb80ee6500696010c58f5b4139edf0");
//	}
}
